package com.augurit.gzcgwsp.webservice.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.Page;

import com.augurit.gzcgwsp.state.dao.StateBaseDao;
@Repository
public class WebseriveDataDao extends StateBaseDao{

	/**
	 * 分页查询
	 * @param page
	 * @param map
	 * @return
	 */
	public List searchByPage(String sql,Page page, Map map) {
		StringBuffer sb=new StringBuffer();
	    sb.append("  select * from ("+"\r");
	    sb.append("  select  *   from ("+"\r");
	    sb.append(" select  a.*,rownum num from ("+"\r");
	    sb.append(sql+") a )"+"\r");
	    sb.append(" where   rownum <="+page.getPageSize()*page.getPageNo()+")"+"\r");
	    sb.append(" where   num> ="+((page.getPageNo()-1)*page.getPageSize()+1)+"\r");
		List list= super.queryForList(sb.toString());
		return list;
	}
	
	public Page findPage(String sql,Page page,Map map) {
		String sizesql=" select count(*) as countnum from ( " + sql+" )";
		List list=this.queryForList(sizesql);
		Map countMap=(Map) list.get(0);
		String total=(String) countMap.get("countnum");
		List datas=searchByPage(sql, page, map);
		if(StringUtils.isNotBlank(total)){
			page.setTotalCount(Long.valueOf(total));
		}else{
			page.setTotalCount(0);
		}
		page.setResult(datas);
		return page;
	}
	
	

}
